package com.example.dllo.keepdemo.tools;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.dllo.keepdemo.base.BaseApplication;
import com.example.dllo.keepdemo.greendao.DaoMaster;
import com.example.dllo.keepdemo.greendao.DaoSession;
import com.example.dllo.keepdemo.greendao.DynamicAttentionDao;
import com.example.dllo.keepdemo.greendao.DynamicFindDao;
import com.example.dllo.keepdemo.greendao.DynamicLatestDao;
import com.example.dllo.keepdemo.greendao.TrainWorkoutsDao;
import com.example.dllo.keepdemo.greendao.TraniPlanDao;


/**
 * Created by Risky on 15/10/29.
 */
public class DaoSingleton {
    private static final String DATABASE_NAME = "keepdatabase.db";

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private TraniPlanDao traniPlanDao;
    private TrainWorkoutsDao trainWorkoutsDao;
    private DynamicFindDao dynamicFindDao;
    private DynamicAttentionDao dynamicAttentionDao;
    private DynamicLatestDao dynamicLatestDao;

    private DaoSingleton() {
        context = BaseApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        return SingletonHolder.instance;
    }
    private static final class SingletonHolder{
        private static final DaoSingleton instance = new DaoSingleton();
    }

    private DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    private SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    private DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    private DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public TraniPlanDao getTraniPlanDao() {
        if (traniPlanDao == null) {
            traniPlanDao = getDaoSession().getTraniPlanDao();
        }
        return traniPlanDao;
    }

    public TrainWorkoutsDao getTrainWorkoutsDao() {
        if (trainWorkoutsDao == null) {
            trainWorkoutsDao = getDaoSession().getTrainWorkoutsDao();
        }
        return trainWorkoutsDao;
    }

    public DynamicFindDao getDynamicFindDao() {
        if (dynamicFindDao == null) {
            dynamicFindDao = getDaoSession().getDynamicFindDao();
        }
        return dynamicFindDao;
    }

    public DynamicAttentionDao getDynamicAttentionDao() {
        if (dynamicAttentionDao == null) {
            dynamicAttentionDao = getDaoSession().getDynamicAttentionDao();
        }
        return dynamicAttentionDao;
    }
    public DynamicLatestDao getDynamicLatestDao() {
        if (dynamicLatestDao == null) {
            dynamicLatestDao = getDaoSession().getDynamicLatestDao();
        }
        return dynamicLatestDao;
    }

}
